package project.teg.views;

import project.teg.R;
import project.teg.common.types.DeviceType;
import project.teg.common.types.HandlingType;
import project.teg.configuration.Global;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.widget.Toast;

public class OptionsServer extends PreferenceActivity implements
		OnPreferenceClickListener, OnPreferenceChangeListener {

	public final static String VRIJEME_OPCIJA = "stg_chronometer";
	public final static String RAZINA_OPCIJA = "stg_level";
	public final static String REZULTAT_OPCIJA = "stg_score";
	public final static String OPIS_OPCIJA = "stg_description";
	public final static String maxrazina_OPCIJA = "stg_maxlevel";
	public final static String deviceType_OPTION = "stg_deviceType";
	public final static String handlingType_OPTION = "stg_handlingType";

	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.optionsglobal);
		setTitle("Global options");
		Global.GetAllValues(this);

		CheckBoxPreference cbp = (CheckBoxPreference) findPreference(REZULTAT_OPCIJA);
		cbp.setChecked(Global.showScore);
		cbp.setOnPreferenceClickListener(this);

		cbp = (CheckBoxPreference) findPreference(VRIJEME_OPCIJA);
		cbp.setChecked(Global.showChronometer);
		cbp.setOnPreferenceClickListener(this);

		cbp = (CheckBoxPreference) findPreference(RAZINA_OPCIJA);
		cbp.setChecked(Global.showLevel);
		cbp.setOnPreferenceClickListener(this);

		cbp = (CheckBoxPreference) findPreference(OPIS_OPCIJA);
		cbp.setChecked(Global.showDescription);
		cbp.setOnPreferenceClickListener(this);

		EditTextPreference etp = (EditTextPreference) findPreference(maxrazina_OPCIJA);
		etp.setText(String.valueOf(Global.maxLevels));
		etp.setSummary(String.valueOf(Global.maxLevels));
		etp.setOnPreferenceChangeListener(this);

		ListPreference lp = (ListPreference) findPreference(deviceType_OPTION);
		lp.setValue(String.valueOf(Global.deviceType.ordinal()));
		lp.setSummary(lp.getEntry());
		lp.setOnPreferenceChangeListener(this);

		lp = (ListPreference) findPreference(handlingType_OPTION);
		lp.setValue(String.valueOf(Global.handlingType.ordinal()));
		lp.setSummary(lp.getEntry());
		lp.setOnPreferenceChangeListener(this);
	}

	@Override
	public boolean onPreferenceClick(Preference arg0) {

		if (arg0 instanceof CheckBoxPreference) {
			CheckBoxPreference cbp = (CheckBoxPreference) arg0;
			if (cbp.getKey().equals(VRIJEME_OPCIJA)) {
				Global.showChronometer = cbp.isChecked();
			} else if (cbp.getKey().equals(REZULTAT_OPCIJA)) {
				Global.showScore = cbp.isChecked();
			} else if (cbp.getKey().equals(RAZINA_OPCIJA)) {
				Global.showLevel = cbp.isChecked();
			} else if (cbp.getKey().equals(OPIS_OPCIJA)) {
				Global.showDescription = cbp.isChecked();
			}
			Toast.makeText(this, "check", Toast.LENGTH_SHORT).show();
		}
		Global.SeveAllValues(this);
		return false;
	}

	@Override
	public boolean onPreferenceChange(Preference arg0, Object arg1) {
		if (arg0 instanceof EditTextPreference) {
			EditTextPreference etp = (EditTextPreference) arg0;
			Global.maxLevels = Integer.parseInt(etp.getEditText().getText()
					.toString());
			etp.setSummary(etp.getEditText().getText().toString());
		} else if (arg0 instanceof ListPreference) {
			ListPreference lp = (ListPreference) arg0;
			if (lp.getKey().equals(deviceType_OPTION)) {
				switch (Integer.parseInt(String.valueOf(arg1))) {
				case 0:
					Global.deviceType = DeviceType.Mouse;
					break;
				case 1:
					Global.deviceType = DeviceType.TouchScreen;
					break;
				case 2:
					Global.deviceType = DeviceType.MobileDevice;
					break;

				default:
					break;
				}
			}
			else{
				switch (Integer.parseInt(String.valueOf(arg1))) {
				case 0:
					Global.handlingType = HandlingType.OneHolding_and_Operating;
					break;
				case 1:
					Global.handlingType = HandlingType.OneHolding_OneOperating;
					break;
				case 2:
					Global.handlingType = HandlingType.TwoHoldind_and_Operating;
					break;
				default:
					break;
				}
			}
			lp.setValue(String.valueOf(arg1));
			lp.setSummary(lp.getEntry());

		}
		Global.SeveAllValues(this);
		return true;
	}
}
